package com.example.zhongcao.utils;


import org.apache.ibatis.jdbc.SQL;

import javax.xml.crypto.Data;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.Date;

import static com.alibaba.fastjson.PropertyNamingStrategy.CamelCase;

public class UpdateSQLProvider {

    public String getUpdateSql(String table, Object source,String condition) throws IllegalAccessException {
        SQL sql=new SQL();
        sql.UPDATE(table);
        Field[] fields=source.getClass().getDeclaredFields();
        for (Field f:fields) {
            String tab=f.getName();
            f.setAccessible(true);
            Object value=f.get(source);
            Object sqlvalue=getSqlValue(value);
            if(tab.equals("goodNum")){
                tab="good_num";
            }
            if(tab.equals("createTime")){
                tab="create_time";
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
                Date time=new Date();
                String currentTime=df.format(time);
                sql.SET(tab+"="+sqlvalue);
                continue;
            }

            sql.SET(tab+"="+sqlvalue);
        }
        sql.WHERE(condition);
        return sql.toString();
    }
    private Object getSqlValue(Object value){
        if(value instanceof String){
            return "'"+value+"'";
        }else{
            return value;
        }
    }

}
